package com.edu.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.admin.entity.SysSchool;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.enums.UseTypeEnum;
import com.edu.admin.service.SchoolService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.PhaseVo;
import com.edu.admin.vo.SchoolVo;
import com.github.pagehelper.PageInfo;

/**
 * 学校管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/school", name = "学校管理")
public class SchoolController {
	
	@Autowired
	private SchoolService	schoolService;
	
	/**
	 *
	 * @apiGroup school
	 * @api {get} /school/list 获取可用学校列表
	 * @apiVersion 1.0.0
	 * @apiName getSchoolList
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
			{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "schoolId": 8,
		            "schoolName": "测试学校8",
		            "shortName": "8",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110102",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "西城区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 3,
		                    "schoolId": 8,
		                    "phaseId": 1,
		                    "phaseName": "小学"
		                },
		                {
		                    "schoolPhaseId": 4,
		                    "schoolId": 8,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                }
		            ]
		        },
		        {
		            "schoolId": 9,
		            "schoolName": "测试学校9",
		            "shortName": "9",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110103",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "崇文区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 5,
		                    "schoolId": 9,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                },
		                {
		                    "schoolPhaseId": 6,
		                    "schoolId": 9,
		                    "phaseId": 3,
		                    "phaseName": "高中"
		                }
		            ]
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "list", name = "可用学校列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/school/list')")
    public RestResult getSchoolList(@RequestParam(value = "provinceCode", required = false) String provinceCode,
			@RequestParam(value = "cityCode", required = false) String cityCode,
			@RequestParam(value = "districtCode", required = false) String districtCode,
			@RequestParam(value = "schoolName", required = false) String schoolName)
    		throws Exception {
    	
    	List<SchoolVo> schoolList = schoolService.getSchoolList(provinceCode, cityCode, districtCode, schoolName);
        
        return RestResult.ok(schoolList);
    }
    
    /**
	 *
	 * @apiGroup school
	 * @api {get} /school/listPage 获取可用学校列表 (分页)
	 * @apiVersion 1.0.0
	 * @apiName getSchoolListPage
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "schoolId": 8,
		            "schoolName": "测试学校8",
		            "shortName": "8",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110102",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "西城区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 3,
		                    "schoolId": 8,
		                    "phaseId": 1,
		                    "phaseName": "小学"
		                },
		                {
		                    "schoolPhaseId": 4,
		                    "schoolId": 8,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                }
		            ]
		        },
		        {
		            "schoolId": 9,
		            "schoolName": "测试学校9",
		            "shortName": "9",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110103",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "崇文区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 5,
		                    "schoolId": 9,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                },
		                {
		                    "schoolPhaseId": 6,
		                    "schoolId": 9,
		                    "phaseId": 3,
		                    "phaseName": "高中"
		                }
		            ]
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "listPage", name = "可用学校列表 (分页)", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/school/listPage')")
    public RestResult getSchoolListPage(@RequestParam(value = "provinceCode", required = false) String provinceCode,
			@RequestParam(value = "cityCode", required = false) String cityCode,
			@RequestParam(value = "districtCode", required = false) String districtCode,
			@RequestParam(value = "schoolName", required = false) String schoolName,
			@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize)
    		throws Exception {
    	
    	PageInfo<SchoolVo> pageInfo = schoolService.getSchoolListPage(pageNum, pageSize, provinceCode, cityCode, districtCode, schoolName);
        
        return RestResult.ok(pageInfo);
    }
    
    /**
	 *
	 * @apiGroup school
	 * @api {get} /school/listAllPage 获取所有学校列表（包括停用学校） (分页)
	 * @apiVersion 1.0.0
	 * @apiName getAllSchoolListPage
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "schoolId": 8,
		            "schoolName": "测试学校8",
		            "shortName": "8",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110102",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "西城区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 3,
		                    "schoolId": 8,
		                    "phaseId": 1,
		                    "phaseName": "小学"
		                },
		                {
		                    "schoolPhaseId": 4,
		                    "schoolId": 8,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                }
		            ]
		        },
		        {
		            "schoolId": 9,
		            "schoolName": "测试学校9",
		            "shortName": "9",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110103",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "崇文区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 5,
		                    "schoolId": 9,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                },
		                {
		                    "schoolPhaseId": 6,
		                    "schoolId": 9,
		                    "phaseId": 3,
		                    "phaseName": "高中"
		                }
		            ]
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "listAllPage", name = "所有学校列表 (分页)", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/school/listAllPage')")
    public RestResult getAllSchoolListPage(@RequestParam(value = "provinceCode", required = false) String provinceCode,
			@RequestParam(value = "cityCode", required = false) String cityCode,
			@RequestParam(value = "districtCode", required = false) String districtCode,
			@RequestParam(value = "schoolName", required = false) String schoolName,
    		@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize) 
			throws Exception {

		PageInfo<SchoolVo> pageInfo = schoolService.getAllSchoolListPage(pageNum,pageSize, provinceCode, cityCode, districtCode, schoolName);
        return RestResult.ok(pageInfo);
    }
			
    /**
	 *
	 * @apiGroup school
	 * @api {get} /school/listAll 获取所有学校列表（包括停用学校）
	 * @apiVersion 1.0.0
	 * @apiName getAllSchoolList
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "schoolId": 8,
		            "schoolName": "测试学校8",
		            "shortName": "8",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110102",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "西城区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 3,
		                    "schoolId": 8,
		                    "phaseId": 1,
		                    "phaseName": "小学"
		                },
		                {
		                    "schoolPhaseId": 4,
		                    "schoolId": 8,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                }
		            ]
		        },
		        {
		            "schoolId": 9,
		            "schoolName": "测试学校9",
		            "shortName": "9",
		            "provinceCode": "110000",
		            "cityCode": "110100",
		            "districtCode": "110103",
		            "address": null,
		            "homepageUrl": null,
		            "logo": null,
		            "descript": null,
		            "classesNum": "0",
		            "provinceName": "北京市",
		            "cityName": "市辖区",
		            "districtName": "崇文区",
		            "useType": 0,
		            "schoolPhaseList": [
		                {
		                    "schoolPhaseId": 5,
		                    "schoolId": 9,
		                    "phaseId": 2,
		                    "phaseName": "初中"
		                },
		                {
		                    "schoolPhaseId": 6,
		                    "schoolId": 9,
		                    "phaseId": 3,
		                    "phaseName": "高中"
		                }
		            ]
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "listAll", name = "所有学校列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/school/listAll')")
    public RestResult getAllSchoolList(@RequestParam(value = "provinceCode", required = false) String provinceCode,
			@RequestParam(value = "cityCode", required = false) String cityCode,
			@RequestParam(value = "districtCode", required = false) String districtCode,
			@RequestParam(value = "schoolName", required = false) String schoolName)
    		throws Exception {
    	
    	List<SchoolVo> schoolList = schoolService.getAllSchoolList(provinceCode, cityCode, districtCode, schoolName);
        
        return RestResult.ok(schoolList);
    }		
    
    /**
	 *
	 * @apiGroup school
	 * @api {post} /school/save 保存学校信息
	 * @apiVersion 1.0.0
	 * @apiName save
	 *
	 * @apiParam {SysSchool} school 学校信息
	 * @apiParamExample 请求示例（json）
	 * {
		  "schoolName":"学校管理员"
		
		} 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "schoolId": 3
		    }
		}
	 *
	 *
	 */
    @PreAuthorize("hasAuthority('/school/save')")
	@RequestMapping(value = "save", name = "保存学校信息", method = RequestMethod.POST)
	public RestResult save(@Valid @RequestBody SysSchool school) throws Exception{
		Map<String, Object> result = new HashMap<>();
		
		result = schoolService.saveSchool(school);
	
		return RestResult.ok(result);
	}
	
	/**
	 *
	 * @apiGroup school
	 * @api {get} /school/info 获取学校信息
	 * @apiVersion 1.0.0
	 * @apiName getSchool
	 *
	 * @apiParam {Long} schoolId 学校id
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "schoolId": 8,
		        "schoolName": "测试学校8",
		        "shortName": "8",
		        "provinceCode": "110000",
		        "cityCode": "110100",
		        "districtCode": "110102",
		        "address": null,
		        "homepageUrl": null,
		        "logo": null,
		        "descript": null,
		        "classesNum": "0",
		        "teacherNum": "0",
		        "studentNum": "0",
		        "provinceName": "北京市",
		        "cityName": "市辖区",
		        "districtName": "西城区",
		        "useType": 0,
		        "schoolPhaseList": [
		            {
		                "schoolPhaseId": 3,
		                "schoolId": 8,
		                "phaseId": 1,
		                "phaseName": "小学"
		            },
		            {
		                "schoolPhaseId": 4,
		                "schoolId": 8,
		                "phaseId": 2,
		                "phaseName": "初中"
		            }
		        ]
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "info", name = "学校信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/school/info')")
	public RestResult getSchool(@RequestParam Long schoolId) throws Exception{
		SchoolVo schoolVo = null;
		
		schoolVo = schoolService.getSchool(schoolId);
		
		return RestResult.ok(schoolVo);
	}
	
	/**
	 *
	 * @apiGroup school
	 * @api {post} /school/delete 删除学校
	 * @apiVersion 1.0.0
	 * @apiName deleteSchool
	 *
	 * @apiParam {Map} dataMap 
	 * @apiParamExample 请求示例（json）
	 * {
		    "schoolId": 2
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	  @RequestMapping(value="/delete" , method = RequestMethod.POST)
	  @PreAuthorize("hasAuthority('/school/delete')")
	  public RestResult deleteSchool(@RequestBody Map<String,Object> dataMap) throws Exception {
	   	   
	      	Long schoolId = MapUtils.getLong(dataMap, "schoolId");
	      	
	      	if (schoolId == null || schoolId<=0)
	      	{
	      		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
	      	}
	  	    			
		  	int isOk = schoolService.deleteSchool(schoolId);
		  	if (isOk>0)
			{
				return RestResult.ok(isOk);
			}
			else
			{
				return RestResult.error();
			}
	  }
	  
	  /**
		 *
		 * @apiGroup school
		 * @api {post} /school/on 启用学校
		 * @apiVersion 1.0.0
		 * @apiName onSchool
		 *
		 * @apiParam {Map} dataMap 
		 * @apiParamExample 请求示例（json）
		 * {
			    "schoolId": 2
			}
		 *
		 * @apiSuccessExample Success-Response:
		 * HTTP/1.1 200
			{
			    "code": 200,
			    "msg": "success",
			    "data": 1
			}
		 *
		 *
		 */
	  @RequestMapping(value="/on" , method = RequestMethod.POST)
	  @PreAuthorize("hasAuthority('/school/on')")
	  public RestResult onSchool(@RequestBody Map<String,Object> dataMap) throws Exception {
	   	   
	      	Long schoolId = MapUtils.getLong(dataMap, "schoolId");
	      	
	      	if (schoolId == null || schoolId<=0)
	      	{
	      		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
	      	}
	  	    			
		  	int isOk = schoolService.useSchool(schoolId,UseTypeEnum.ISUSE.getCode());
		  	if (isOk>0)
			{
				return RestResult.ok(isOk);
			}
			else
			{
				return RestResult.error();
			}
	  }
	  
	  /**
		 *
		 * @apiGroup school
		 * @api {post} /school/off 停用学校
		 * @apiVersion 1.0.0
		 * @apiName offSchool
		 *
		 * @apiParam {Map} dataMap 
		 * @apiParamExample 请求示例（json）
		 * {
			    "schoolId": 2
			}
		 *
		 * @apiSuccessExample Success-Response:
		 * HTTP/1.1 200
			{
			    "code": 200,
			    "msg": "success",
			    "data": 1
			}
		 *
		 *
		 */
	  @RequestMapping(value="/off" , method = RequestMethod.POST)
	  @PreAuthorize("hasAuthority('/school/off')")
	  public RestResult offSchool(@RequestBody Map<String,Object> dataMap) throws Exception {
	   	   
	      	Long schoolId = MapUtils.getLong(dataMap, "schoolId");
	      	
	      	if (schoolId == null || schoolId<=0)
	      	{
	      		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
	      	}
	  	    			
		  	int isOk = schoolService.useSchool(schoolId,UseTypeEnum.NOTUSE.getCode());
		  	if (isOk>0)
			{
				return RestResult.ok(isOk);
			}
			else
			{
				return RestResult.error();
			}
	  }
	  
	  /**
		 *
		 * @apiGroup school
		 * @api {get} /school/list 获取可用学校列表
		 * @apiVersion 1.0.0
		 * @apiName getSchoolList
		 *
		 *
		 *
		 * @apiSuccessExample Success-Response:
		 * HTTP/1.1 200
	
		 *
		 *
		 */
	  @RequestMapping(value = "listPhase", name = "学校学科列表", method = RequestMethod.GET)
	  @PreAuthorize("hasAuthority('/school/listPhase')")
	    public RestResult getSchoolPhaseList(@RequestParam(value = "schoolId", required = false) Long schoolId)
	    		throws Exception {
	    	
	    	List<PhaseVo> phaseList = schoolService.getSchoolPhaseList(schoolId);
	        
	        return RestResult.ok(phaseList);
	    }


}
